import 'dart:convert';
import 'dart:io';
import 'package:dio/dio.dart';

import 'package:flutter/material.dart';

// Import package
import 'package:recorder_wav/recorder_wav.dart';

var filePath;

class recorderWavPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => recorderWavState();
}

uploadRecorder() async {
  print('uploadRecorder');
  var dio = Dio();
  // dio.options.baseUrl = "http://localhost:3000/";

  FormData formData = FormData.from({
    "file": new UploadFileInfo(
        new File("/Users/hanjingwei/Downloads/2019-09-03_03-01-29.wav"),
        "/2019-09-03_03-01-29.wav"),
    "text": "8703.gramx",
    "userId": "602598",
    "subtitleId": "1dc59bae8c2849e08e453f6748ec3ef7",
    "logId": "2c9f808c6cd7e8b9016cf5a5694609d0",
    "type": 1,
  });

  try {
    Response response;
    response = await dio.post(
      "https://www.bettermorph.com/micoservcice/ami/check",
      data: formData,
      onSendProgress: (received, total) {
        if (total != -1) {
          print((received / total * 100).toStringAsFixed(0) + "%");
        }
      },
    );

    print('response');
    print(response);
  } catch (e) {
    print(e);
  }
}

class recorderWavState extends State<recorderWavPage> {
  var filePathStr;

  startRecorder() async {
    //Start recorder
    RecorderWav.startRecorder();
    print('存储成功');
  }

  stopRecorder() async {
    //Stop recorder & get filepath
    filePath = await RecorderWav.StopRecorder();

    setState(() {
      filePathStr = filePath;
    });

    print('filePath');
    print(filePath);
  }

  @override
  void initState() {
//    //Start recorder
//    RecorderWav.startRecorder();
//
//    //Stop recorder & get filepath
//    String filePath = await RecorderWav.StopRecorder();
//
//    //Remove wav file
//    RecorderWav.removeRecorderFile(filePath);

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: new AppBar(
        backgroundColor: Colors.deepOrange,
        elevation: 0.0,
        title: new Text(
          "录播中心",
          style: new TextStyle(
              color: Colors.white,
              fontSize: 18.0,
              fontWeight: FontWeight.normal),
        ),
      ),
      body: new Container(
        width: 500,
        color: Colors.white,
        //padding: const EdgeInsets.only(top: 42.0),
        child: new Column(children: <Widget>[
          RaisedButton(
            child: Text('开始录音'),
            color: Colors.red,
            textColor: Colors.white,
            onPressed: () {
              startRecorder();
            },
          ),
          RaisedButton(
            child: Text('结束录音'),
            color: Colors.red,
            textColor: Colors.white,
            onPressed: () {
              stopRecorder();
            },
          ),
          RaisedButton(
            child: Text('上传录音'),
            color: Colors.red,
            textColor: Colors.white,
            onPressed: () {
              uploadRecorder();
            },
          ),
          filePathStr != null ? Text(filePathStr) : new Container(),
        ]),
      ),
    );
  }
}

class MsgResponse {
  int code; // 状态代码，0 表示没有错误
  Object data; // 数据内容，一般为字符串
  String errmsg; // 错误消息
  MsgResponse([this.code = 0, this.data = null, this.errmsg = ""]);
}
